<?php
/**
 * Created by PhpStorm.
 * User: wangsentao
 * Date: 2018/1/30
 * Time: 上午10:42
 */

namespace app\common\ShaoHuoPay\Gateways\Wechat;

use app\common\ShaoHuoPay\Contracts\GatewayInterface;


abstract class Gateway implements GatewayInterface
{
    /**
     * @var Config
     */
    protected $config;

    /**
     * Gateway constructor.
     * @param Config $config
     */
    public function __construct($config)
    {
        $this->config = $config;
    }

    /**
     * 支付订单
     * @param $endpoint
     * @param array $payload
     * @return mixed
     */
    abstract public function pay($endpoint, array $payload);

    /**
     * 获取交易类型
     * @return mixed
     */
    abstract protected function getTradeType();

    /**
     * 预支付订单
     * @param $endpoint
     * @param $payload
     * @return Collection
     */
    protected function preOrder($endpoint, $payload)
    {
        $payload['sign'] = WxTools::generateSign($payload, $this->config->get('key'));

        //Log::debug('Pre Order:', [$endpoint, $payload]);

        return WxTools::requestApi($endpoint, $payload, $this->config->get('key'));
    }
}